class Play {
  constructor(){
    this.init()
  }
  init() {
    this.audio = window.document.createElement('audio')
    this.now_index = -1
    this.type = 1
    this.musicList = []
  }
  playMusic( index ) {
    if( this.musicList.length > 0 ) {  // 是否有歌曲
      if( index === undefined ) {  // 不传参数处理
        if( this.now_index === -1 ) {
          this.now_index = 0
          this.audio.src = this.musicList[ 0 ]
        }
        if( this.audio.paused )  //  判断是否暂停
          this.audio.play()  //  恢复播放
        else 
          this.audio.pause()  //  暂停播放
      } else if( index >= 0 && index === this.now_index ) {  // 传入的参数是正在播放的那首处理
        if( this.audio.paused )  //  判断是否暂停
          this.audio.play()  //  恢复播放
        else 
          this.audio.pause()  //  暂停播放
      } else if( index >= 0 && index < this.musicList.length ) {  // 传参的参数不是正在播放的那个index
        this.now_index = index
        this.audio.src = this.musicList[ index ]
        this.audio.play()
      }
    }
  }
  preMusic() {  //  切换上一首音乐
    let index = this.now_index - 1
    if( index < 0 ) {
      index = this.musicList.length - 1
    }
    this.setPlay( index )
    return index
  }
  nextMusic() {  //  切换下一首音乐
    var index =this.now_index + 1
    if( index >= this.musicList.length ) {
        index = 0
    }
    this.setPlay( index )
    return index
  }
  formatData( currentTime, duration ) {  //格式化时间
    var currentM = parseInt(currentTime/60);
    if(currentM<10) currentM = '0'+currentM;

    var currentS = parseInt(currentTime%60);
    if(currentS<10) currentS = '0'+currentS;

    var durationM = parseInt(duration/60);
    var durationS = parseInt(duration%60);
    if(durationM<10) durationM = '0'+durationM;
    if(durationS<10) durationS = '0'+durationS;
    
    return {currentTime:currentM+':'+currentS,duration:durationM+':'+durationS};
  }
  onPlay( callback ) {  //  播放器播放监听
    //播放器播放监听
    this.audio.addEventListener('play', () => { 
      callback( this.audio )
    })
  }
  onPause( callback ) {  //  暂停事件
    this.audio.addEventListener('pause', () => { 
      callback( this.audio )
    })
  }
  onLoadedMetaData( callback ) {  //  当播放器加载完元数据的事件：时长，文字。。。
    this.audio.addEventListener('loadedmetadata',function(){
      this.getMusicInfo( callback )
    }.bind(this))
  }
  onMusicUpdate( callback ) {  //  播放器播放位置改变监听
    this.audio.addEventListener("timeupdate", function() {
        this.getMusicInfo( callback )
    }.bind(this))
  }
  getCurrentTime() {
    return this.audio.currentTime
  }
  getPaused() {
    return this.audio.paused
  }
  getEnded() {
    return this.audio.ended
  }
  getRandom() {
    return (Math.random() * ( this.musicList.length - 1 )).toFixed(0)
  }
  getMusicInfo( callback ) {  //  获取播放器的音乐时长信息，传入一个回调函数获取格式化的时间
    var duration = this.audio.duration;
    var currentTime = this.audio.currentTime;
    var data = this.formatData(currentTime, duration);
    callback(currentTime, duration, data);
  }
  getType( ) {
    return this.type
  }
  getMusicList() {
    return this.musicList
  }
  setPlay( index ) {  //  设置播放的音频
    this.audio.src = this.musicList[ index ]
    this.audio.play()
    this.now_index = index
    return true
  }
  setCurrentTime( time ) {  //  设置播放时间
    this.audio.currentTime = time;
  }
  setType( val ) {
    this.type = val
  }
  setMusicList( data ) {
    data instanceof Array ? this.musicList = data : this.musicList = [data]
  }
  
}
export default Play

